#! /bin/sh
#
# Copyright (c) 2006 International Business Machines
#
# This script is modified from the /usr/bin/samadapter script 
# of IBM sam.adapter Software Package
#
#

LOG_LVL=0
ONLINE=0
OFFLINE=3
HB_BIN_DIR=@HA_LIBHBDIR@
TSA_EVENTD=${HB_BIN_DIR}/tsa_eventd
MSG_NOUNAME='Cannot determine OS - exit.'
MSG_NOJAVA='Cannot find Java - exit.'

UNAME=`uname -s`

function logit ()
{
	if [ $LOG_LVL -ge $1 ]
  	then
		logger -i -t $0: "$2"
	fi
}


# function getValue
# get the Value string from key in file
# parameters:
#  $1 File to parse
#  $2 key to search for string
#  $3 ==1, if removal of trailing CR(13) is required
# return:
#  Value: String after "="
#
function getValue ()
{
	R=$(cat $1 | grep ^$2 | sed s/' '//g | sed s/.*=// | sed s/\"//g )
	if [ -z "$R" ]
	then
		R="Cannot get text for $1="
	fi
	if [ $3 -eq 1 ]
	then
		# remove CR
		RCNT=$(echo $R | wc -c)
		RCNT=`expr $RCNT - 1`
		R=$(echo $R | cut -b1-$RCNT)
	fi
	echo $R
}


function getJavaPath () 
{
	JPATH=""
	case "$UNAME" in
		AIX)   JPATH=$(find /usr -name "java[1-9][4-9]*" | grep -v 64 | sort -r | head -1)
		;;
		Linux) JPATH=$pfixed/java
		;;
		*)      echo $MSG_NOUNAME
		;;
	esac
	if [ -n "$JPATH" ]
	then
		JPATH=$JPATH$1
	fi
	echo $JPATH
}

function getStatus () 
{
	RC=0
	PID=$(ps ax | grep -v grep | grep "$SAM_EYE_CATCHER" | grep -v " D " | head -1 | sed s/' '*// | cut -f1 -d" ")
	if [ -z "$PID" ] 
	then
		echo "Adapter is not running."
		RC=$OFFLINE
	else
		echo "Adapter is running [pid: $PID]."
		RC=$ONLINE
	fi
	return $RC
}

function startEventd () 
{
	${TSA_EVENTD} -s
	if [ $? -eq 0 ] ; then
		echo "eventd already started."
		return 0
	fi
	${TSA_EVENTD} &
	${TSA_EVENTD} -s
	if [ $? -eq 3 ] ; then
		echo "Can not start eventd."
		exit 3
	fi

	return 0
}

function stopEventd ()
{
	${TSA_EVENTD} -k
}

function startAdapter ()
{
	getStatus
	RC=$?
	if [ $RC -eq $OFFLINE ] ; then
		if [ "$UNAME" = AIX ] ; then
			MKTEMP=''
			LSOF=''
		else  #linux
			MKTEMP=`whereis mktemp | cut -d' ' -f2 | cut -d' ' -f3`
			LSOF=`whereis lsof | cut -d' ' -f2 | cut -d' ' -f3`
		fi
		rm -f /tmp/$adapter.* 
		# Create a tempfile to capture the first few lines of the output
		if [ -n "$MKTEMP" ] ; then
			TMPFILE=`$MKTEMP -q /tmp/$adapter.XXXXXX`
		else
			# It's better to use mktemp, but we don't have it, we make it cryptic
			RNDM=$(echo $RANDOM)
			TMPFILE=/tmp/$adapter.XXXXXX-RNDM
			touch $TMPFILE
		fi
		# set LIBRAY path for jni parts
		NATIVE_LIBPATH=/lib:/usr/lib
		if [ "$UNAME" = "AIX" ] ; then
			export LIBPATH=$plib:$LIBPATH:$NATIVE_LIBPATH
		else
			export LD_LIBRARY_PATH=$plib:$LD_LIBRARY_PATH:$NATIVE_LIBPATH
		fi
		$pjava/bin/java -$SAM_EYE_CATCHER -cp $jars \
			-Djava.security.auth.login.config=$pcfgm/sam.adapter.jaas.properties \
			-Dcom.ibm.eez.adapter.configFile=$ADAPTER_CFG \
			-Dcom.ibm.eez.adapter.configSSL=$pcfgm/sam.adapter.ssl.properties \
			-Djlog.propertyFileDir=$pcfgm \
			-Djlog.propertyFileName=sam.adapter.jlog.properties com.ibm.eez.adapter.EEZAdapterMain 2>&1 \
			| head -1 > $TMPFILE &

		logit 0 "Started, waiting for $adapter to become ready..."

		if [ -n "$LSOF" ] ; then
			# Check if the tempfile is closed
			$LSOF 2> /dev/null | grep $TMPFILE > /dev/null 2>&1
			RETCODE=$?
			# If the tempfile is not yet closed, sleep for a second and try again
			while [ $RETCODE -ne 1 ];
			do
				sleep 2
				$LSOF 2> /dev/null | grep $TMPFILE > /dev/null 2>&1
				RETCODE=$?
                	 done
		else
                 	sleep 4
              	fi

		# cat the output to the screen and remove the tempfile
		cat $TMPFILE && rm -f $TMPFILE
		echo "$adapter has started."
		logit 0 "$adapter has started."
		RC=0
	else	# already online
		echo "$adapter is not started because it is already running on $RUNS_ON_NODE"
		logit 0 "$adapter is not started because it is already running on $RUNS_ON_NODE"
		RC=0
	fi
        return $RC
}


function stopAdapter()
{
	rm -f /tmp/$adapter.*
	getStatus
	RC=$?
	if [ $RC -eq $ONLINE ]; then
		HOST_NAME=$(getValue $ADAPTER_CFG "eez-remote-contact-hostname" 1)
		PORT=$(getValue $ADAPTER_CFG "eez-remote-contact-port" 1)
		SSLVAL=$(getValue $ADAPTER_CFG "eez-remote-contact-over-ssl" 1)
		SSL=""
		if [ "$SSLVAL" = "true" ] ; then
			SSL="-ssl"
		fi
		$pjava/bin/java -cp $jars \
			-Djava.security.auth.login.config=$pcfgm/sam.adapter.jaas.properties \
			-Dcom.ibm.eez.adapter.configFile=$ADAPTER_CFG \
			-Dcom.ibm.eez.adapter.configSSL=$pcfgm/sam.adapter.ssl.properties \
			-Djlog.propertyFileDir=$pcfgm \
			-Djlog.propertyFileName=sam.adapter.jlog.properties com.ibm.eez.adapter.EEZAdapterCmd\
		 	-terminate -host $HOST_NAME -port $PORT $SSL >/dev/null 2>&1
			logit 2 "Waiting 10 seconds after sending stop to $adapter  PID $PID..."
			logit 0 "$adapter with PID $PIDSAVE stopped"
			RC=0
	else
		echo "$adapter is not running"
		logit 2 "$adapter is not running"
		RC=0
	fi
	return $RC
}

adapter=samadapter
SAM_EYE_CATCHER=Dcom.ibm.sam.samadapter
pprod=IBM/tsamp/sam
pfixed=/opt/$pprod
plib=$pfixed/lib
pcfgm=/etc$pfixed/cfg

jars=$plib/eezadapter.jar:$plib/eezmsgs.jar:$plib/eezmsgs_lang.jar:$plib/eezsdk.jar:$plib/eezutils.jar:$plib/evd.jar:$plib/log.jar:$plib/jlog.jar:$plib/jffdc.jar:$plib/tivolicommondir.jar:/usr/sbin/rsct/codebase/rmcapi.jar:/usr/sbin/rsct/codebase:$plib/samrmc.jar:$plib/samfla.jar:$plib/sammsgs.jar:$plib/linuxhafla.jar

pit=/var/ibm/tivoli/common
pitlogcfg=/etc/ibm/tivoli/common/cfg
peezlogs=eez/logs
peezffdc=eez/ffdc
tivoli_common_dir=""

if [ -r "$pitlogcfg/log.properties" ] ; then
	# read the tivoli_common_dir attribute
	. $pitlogcfg/log.properties
fi
if [ -z "$tivoli_common_dir" ] ; then
	logit 0 "Warning - 'tivoli_common_dir' not specified in $pitlogcfg/log.properties"
	logit 0 "Will use default: $pit"
	tivoli_common_dir=$pit
fi

ADAPTER_CFG=$pcfgm/sam.adapter.properties
if [ -z "$ADAPTER_CFG" ] ; then
	echo "$adapter configuration '$ADAPTER_CFG' does not exist - exit."
	exit 1
fi

###########################################
# main begin
###########################################

ACTION=${1}
RC=0
PID=""
RUNS_ON_NODE=""

POLICY_FILE=$pcfgm/sam.adapter.conf
if [ ! -r "$POLICY_FILE" ] ; then
	echo "$POLICY_FILE does not exist - exit."
fi

pjava=$(getJavaPath /jre)
if [ -z "$pjava" ]
then
	echo $MSG_NOJAVA
	exit 1
fi


case ${ACTION} in
	start)	startEventd
		startAdapter
		RC=$?
		;;
	stop)	stopAdapter
		RC=$?
		stopEventd
		;;
	status)	getStatus	
		RC=$?
		;;
esac
exit $RC

